<?php

namespace App\Facades\Express;

use App\ApiKey;
use App\data;
use App\DataSign;
use Illuminate\Support\Facades\Http;
use function config;

class Express
{
    protected $EBusinessID;
    protected $ApiKey;
    protected $mode;
    public function __construct()
    {
        $config = config('express');
        $this->EBusinessID = $config['EBusinessID'];
        $this->ApiKey = $config['ApiKey'];
        $this->mode = $config['mode']??'product';
    }

    /**
     * 即时查询快递足迹
     */
    public function track($ShipperCode,$LogisticCode)
    {
        // 组装应用级参数
        $requestData= "{".
            "'CustomerName': '',".
            "'OrderCode': '',".
            "'ShipperCode': '{$ShipperCode}',".
            "'LogisticCode': '{$LogisticCode}',".
            "}";
        //发送请求
        $result = Http::asForm()->post(
            $this->url('track'),
            $this->formatReqData($requestData,'1002'));

        return $this->formatResData($result);
    }

    /**
     * 格式化请求参数
     */
    protected function formatReqData($requestData,$RequestType)
    {
        // 组装系统级参数
        $datas = array(
            'EBusinessID' => $this->EBusinessID,
            'RequestType' => $RequestType, //免费即时查询接口指令1002/在途监控即时查询接口指令8001/地图版即时查询接口指令8003
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData);
        return $datas;
    }
    /**
     * 格式化响应参数
     */
    protected function formatResData($result)
    {
        $result = json_decode($result,true);
        //api服务器报错
        if($result['Success'] == false){
            return $result['ResponseData'];
        }
        $result2 = json_decode($result['ResponseData'],true);
        //请求成功，但是未请求到数据，请求的参数有问题
        if($result2['Success'] == false){
            return $result2['Reason'];
        }
        return $result2;
    }

    /**
     * 返回的Api URL
     */
    protected function url($type)
    {
        $url = [
            'track' => [
                'product' => 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx',
                'dev' => 'https://www.kdniao.com/UserCenter/v2/SandBox/SandboxHandler.ashx?action=CommonExcuteInterface'
            ]
        ];
        return $url[$type][$this->mode];
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param ApiKey ApiKey
     * @return DataSign签名
     */
    protected function encrypt($data)
    {
        return urlencode(base64_encode(md5($data.$this->ApiKey)));
    }

}
